#include<bits/stdc++.h>
using namespace std;
#define piii pair<int,pair<int,int>>
#define fi first
#define se second
const int N=102;
int T,n,dp[N][N][N],a[N],ans;
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>T;
    while(T--){
        queue<piii>q;
        cin>>n;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        ans=0,memset(dp,-1,sizeof dp),dp[0][0][0]=0,q.push({0,{0,0}});
        for(int i=1;i<=n;i++){
            piii u=q.front();q.pop();
            if(dp[u.fi][u.se.fi][u.se.se]+(a[i]==u.se.fi)*u.se.fi>dp[u.fi+1][a[i]][u.se.se])
                dp[u.fi+1][a[i]][u.se.se]=dp[u.fi][u.se.fi][u.se.se]+(a[i]==u.se.fi)*u.se.fi,q.push({u.fi+1,{a[i],u.se.se}});
            if(dp[u.fi][u.se.fi][u.se.se]+(a[i]==u.se.se)*u.se.se>dp[u.fi+1][u.se.fi][a[i]])
                dp[u.fi+1][a[i]][u.se.se]=dp[u.fi][u.se.fi][u.se.se]+(a[i]==u.se.se)*u.se.se,q.push({u.fi+1,{u.se.fi,a[i]}});
            ans=max(ans,max(dp[u.fi+1][a[i]][u.se.se],dp[u.fi+1][a[i]][u.se.se]));
        }cout<<ans<<endl;
    }return 0;
}
